package snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

//游戏主代码
public class GamePanel extends JPanel implements KeyListener, ActionListener {

    int length;//蛇的长度
    int[] snakeX=new int[600];//蛇的坐标X
    int[] snakeY=new int[600];//蛇的坐标Y
    String direction;  //蛇的方向
    boolean isStart=false;//游戏是否开始
    Timer timer=new Timer(100,this);//定时器

    //定义一个食物
    int foodx;
    int foody;
    Random random= new Random();
    boolean isFail=false;//死亡判断
    int score;//积分系统

    //构造器
    public GamePanel(){
        init();

        //获得键盘的监听事件
        this.setFocusable(true);
        this.addKeyListener(this);
        timer.start(); //让时间动起来
    }

    //初始化
    public void init(){
        length = 3;
        snakeX[0]=100;snakeY[0]=100;//头部坐标
        snakeX[1]=75;snakeY[1]=100;//第一个身体坐标
        snakeX[2]=50;snakeY[2]=100;//第二个身体坐标
        direction="R"; //R:右  L:左  U:上  D:下

        foodx=25+25*random.nextInt(34);
        foody=75+25*random.nextInt(24);

        score=0;
    }

    //画板：画界面，画蛇   Graphics：画笔
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);//清屏
        this.setBackground(Color.white);
        Data.header.paintIcon(this,g,25,11);//绘制头部的广告栏
        g.fillRect(25,75,850,600);//绘制游戏区域

        //画一条静态的小蛇
        switch (direction) {
            case "R":
                Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
                break;
            case "L":
                Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
                break;
            case "U":
                Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
                break;
            case "D":
                Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);
                break;
        }

        //蛇的身体长度通过length来控制
        for (int i = 1; i < length; i++) {
            Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
        }

        //画积分
        g.setColor(Color.orange);
        g.setFont(new Font("微软雅黑",Font.BOLD,22));//设置字体
        g.drawString("长度:"+length,700,32);
        g.drawString("分数:"+score,700,55);

        //画食物
        Data.food.paintIcon(this,g,foodx,foody);

        //游戏提示：是否开始
        if (!isStart){
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));//设置字体
            g.drawString("按下空格开始游戏",300,300);
        }

        //失败提示
        if (isFail){
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));//设置字体
            g.drawString("游戏失败，按下空格重新开始",200,300);
        }
    }


    //接收键盘的输入：监听
    @Override
    public void keyPressed(KeyEvent e) {
        //键盘按下，未释放
        //获取按下的键盘是那个键
        int keyCode=e.getKeyCode();

        if (keyCode==KeyEvent.VK_SPACE){//如果按下的是空格键
            if (isFail){  //失败，游戏再来一遍
                isFail=false;
                init();   //重新初始化游戏
            }else {
                isStart = !isStart; //暂停游戏
            }
            repaint();//刷新界面
        }

        //键盘控制走向
        if (keyCode==KeyEvent.VK_A &&!direction.equals("R")){
            direction="L";
        }else if (keyCode==KeyEvent.VK_D &&!direction.equals("L")){
            direction="R";
        }else if (keyCode==KeyEvent.VK_W &&!direction.equals("D")){
            direction="U";
        }else if (keyCode==KeyEvent.VK_S &&!direction.equals("U")){
            direction="D";
        }

    }

    //定时器，监听时间，帧：执行定时操作
    @Override
    public void actionPerformed(ActionEvent e) {

        if (isStart&& !isFail){  //如果游戏处于开始状态，并且游戏没有失败
            for (int i = length -1; i >0 ; i--) {  //除了头部，身体都向前移动
                snakeX[i]=snakeX[i-1];
                snakeY[i]=snakeY[i-1];
            }

            //通过控制方向让头部移动
            switch (direction) {
                case "R":
                    snakeX[0] = snakeX[0] + 25; //头部移动
                    if (snakeX[0] > 850) {
                        isFail = true; }//边界判断
                    break;
                case "L":
                    snakeX[0] = snakeX[0] - 25; //头部移动
                    if (snakeX[0] < 25) {
                        isFail = true; }//边界判断
                    break;
                case "U":
                    snakeY[0] = snakeY[0] - 25; //头部移动
                    if (snakeY[0] < 75) {
                        isFail = true; }//边界判断
                    break;
                case "D":
                    snakeY[0] = snakeY[0] + 25; //头部移动
                    if (snakeY[0] > 650) {
                        isFail = true; }//边界判断
                    break;
            }

            //如果小蛇的头和食物坐标重合了
            if (snakeX[0]==foodx && snakeY[0]==foody){
                length++; //长度+1
                snakeX[length - 1] = snakeX[length - 2] * 2 - snakeX[length - 3];
                snakeY[length - 1] = snakeY[length - 2] * 2 - snakeY[length - 3];
                score=score+10;

                //重新生成食物
                foodx=25+25*random.nextInt(34);
                foody=75+25*random.nextInt(24);
            }

            //结束判断
            for (int i = 1; i < length; i++) {
                if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
                    isFail = true;
                    break;
                }
            }

            repaint();//刷新界面
        }

        timer.start(); //让时间动起来

    }

    @Override
    public void keyTyped(KeyEvent e) {
        //键盘按下，弹起：敲击
    }

    @Override
    public void keyReleased(KeyEvent e) {
        //释放某个键

    }


}
